﻿<?php
/*
 * 创建时间：2010-05-09
 * 描述：连接数据库
 * 版本：1.0.0.1
 * 版权所有：河南诺特信息技术有限公司
 */
class DB_Oracle {

	private $dbstr1 ="(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =123.14.10.152)(PORT = 1521))
	(CONNECT_DATA =
	(SERVER = DEDICATED)
	(SERVICE_NAME = orcl)))";
	private $Host, $User, $Password, $DBname, $Pconnect;	//主机, 账户, 密码, 数据库名, 是否持久链接数据库
	public  $Charset, $conn;								//编码, 数据库连接句柄

	function __construct($User = 'gps', $Password = 'gps', $DBname = 'orcl', $Pconnect = false, $Host = 'localhost', $Charset = 'utf8'){
		$this->Host     = $Host;
		$this->User     = $User;
		$this->Password = $Password;
		$this->DBname   = "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.20.58)(PORT = 1521))
	(CONNECT_DATA =
	(SERVER = DEDICATED)
	(SERVICE_NAME = orcl)))";
		$this->Pconnect = $Pconnect;
		$this->Charset  = $Charset;

		$this->Connect($this->User, $this->Password, $this->DBname, $this->Host, $this->Pconnect, $this->Charset);
	}

	//建立数据库连接
	function Connect($User, $Password, $DBname, $Host, $Pconnect, $Charset){
		$ConnMethod = $Pconnect ? 'oci_pconnect' : 'oci_connect';
		@$conn = $ConnMethod($User,$Password, $DBname) or die ($this->error());
		return $this->conn = $conn ? $conn : false;
	}

	//普通执行sql语句
	function query($sql){
		oci_execute($query = oci_parse($this->conn, $sql));
		return $query;
	}
	
	function que($sql){
		$query = oci_parse($this->conn, $sql);
		return $query;
	}

	//读取数据 以数组的形式显示
	function fetch_array($sql, $type = 'assoc'){
        $result = $this->query ($sql);
        if ($type == 'assoc'){
			return @oci_fetch_assoc($result);
        }else if ($type == 'row'){
            return @oci_fetch_row($result);		//本函数的字段名区分大小写
        }else{
            return @oci_fetch_array($result);
        }
    }

	function fetch_row($sql){
		 return $this->fetch_array($sql,"row");
	}

	function fetch($query){
		return oci_fetch_array($query);
	}
	//读取全部数据
    function fetch_all_array($sql)
	{
        $records = array();
        $type	 = "assoc";
        $result  = $this->query($sql);
        if ($type == 'assoc'){
        	while ($row = @oci_fetch_assoc($result)){
               $records[] = $row;
            }
        }else if($type == 'row'){
            while($row = @oci_fetch_row($result)){
               $records[] = $row;
			}
        }else{
            while($row = @oci_fetch_array($result)){
               $records[] = $row;
            }
        }
    	return $records;
	}

	//读取一行数据
    function fetch_one_array($sql, $type = ''){
        //type='' 表示调用mysql_fetch_array();
        $record = $this->fetch_array($sql, $type);
        return $record;
    }

	//返回结果集中的行数
    function num_rows($query){
        return @oci_num_rows($query);
    }

	//返回一条记录
    function fetch_one($sql, $type = 'assoc'){
          $record = $this->fetch_one_array($sql, 'row');
          return $record [0];
    }

    //取得前一次mysql操作所影想的记录数
    function affected_rows($result){
         $num = count ($result);
         return $num;
    }

    //关闭 数据库连接 成功返回true
    function close(){
		return @oci_close ();
	}

	//显示sql错误
	function Error(){
		return print_r(oci_error());
	}

	function fetch_one_Into($sql){
		$result  = $this->query($sql);
		 while(OCIFetchInto($result,$row, OCI_ASSOC+OCI_RETURN_LOBS)) {
			$records=$row;
        }
        return $records;
	}

	//读取clob字段
	function fetchInto($sql){
		 $result  = $this->query($sql);
		 while(OCIFetchInto($result,$row, OCI_ASSOC+OCI_RETURN_LOBS)) {
			$records[]=$row;
        }
        return $records;
	}


	//释放结果内存
    function free_result($query){
		$bool = @oci_free_statement($query);
        return $bool;
    }
    //跳转页
    function forword($url){
		echo "<script type='text/javascript'>";
        echo "window.location='" . $url . "'";
        echo "</script>";
        exit ();
    }
}
?>